package com.echo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.echo.dao.TopicDAO;
import com.echo.dao.TopicListViewDAO;
import com.echo.entity.bean.PageResult;
import com.echo.entity.dto.*;
import com.echo.entity.pojo.Topic;
import com.echo.entity.vo.TopicListVO;
import com.echo.service.TopicService;
import com.echo.utils.DateTimeTransferUtil;
import com.echo.utils.PageParamCheckUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/***
 * @date   2021/8/2 0002 15:19
 * @author JackeyTower
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicDAO, Topic> implements TopicService {
    private TopicDAO topicDAO;
    private PageParamCheckUtil pageParamCheckUtil;
    private TopicListViewDAO topicListViewDAO;

    public TopicServiceImpl(TopicDAO topicDAO, PageParamCheckUtil pageParamCheckUtil, TopicListViewDAO topicListViewDAO) {
        this.topicDAO = topicDAO;
        this.pageParamCheckUtil = pageParamCheckUtil;
        this.topicListViewDAO = topicListViewDAO;
    }

    @Override
    public void addTopic(TopicAddDTO topicAddDTO) {
        //创建一个新话题
        Topic topic = new Topic();
        BeanUtils.copyProperties(topicAddDTO,topic);

        //赋初值
        topic.setCreateTime(Long.parseLong(DateTimeTransferUtil.getNowTimeStamp()));
        topicDAO.insert(topic);
    }

    @Override
    public void deleteTopicByCreator(TopicDeleteDTO topicDeleteDTO) throws Exception {
        Topic topic = topicDAO.selectOne(new QueryWrapper<Topic>().eq("id", topicDeleteDTO.getTopicId()).eq("user_id", topicDeleteDTO.getUserId()));
        if (topic==null){
           throw new Exception("话题不存在");
        }
        topicDAO.deleteById(topic.getId());
    }

    @Override
    public void updateTopicByCreator(TopicUpdateDTO topicUpdateDTO) {
        topicDAO.updateTopicByCreator(topicUpdateDTO.getTopicId(),topicUpdateDTO.getContent());
    }

    @Override
    public PageResult<TopicListVO> getAllPaginatedTopic(PageParamDTO pageParamDTO) {
        Page<Object> page = PageHelper.startPage(pageParamDTO.getCp(), pageParamDTO.getPs(), pageParamCheckUtil.CheckOrder(pageParamDTO.getOrder()));
        List<TopicListVO> topicListVOS = topicDAO.selectTopicListVO();

        return new PageResult<>(page.getPages(),page.getTotal(),topicListVOS);
    }

    @Override
    public PageResult<TopicListVO> getAllPaginatedTopicListByParam(TopicQueryByParamDTO topicQueryByParamDTO) {
        PageParamDTO pageParamDTO = topicQueryByParamDTO.getPageParamDTO();
        QueryWrapper<TopicListVO> topicListVOQueryWrapper = new QueryWrapper<>();
        if (topicQueryByParamDTO.getTitle()!=null){
            topicListVOQueryWrapper.eq("title",topicQueryByParamDTO.getTitle());
        }
        if (topicQueryByParamDTO.getTopicType()!=null){
            topicListVOQueryWrapper.eq("topic_type",topicQueryByParamDTO.getTopicType());
        }
        if (topicQueryByParamDTO.getUserNickname()!=null){
            topicListVOQueryWrapper.eq("user_nickname",topicQueryByParamDTO.getUserNickname());
        }

        Page<Object> page = PageHelper.startPage(pageParamDTO.getCp(), pageParamDTO.getPs(), pageParamCheckUtil.CheckOrder(pageParamDTO.getOrder()));
        List<TopicListVO> topicListVOS = topicListViewDAO.selectList(topicListVOQueryWrapper);

        return new PageResult<>(page.getPages(),page.getTotal(),topicListVOS);
    }

    @Override
    public void giveLikeForTopic(String topicId) {
        topicDAO.riseLikesForTopic(topicId);
    }

}
